﻿using EnetaMvc.Core.Domain.Content;
using EnetaMvc.Core.Repositories;
using EnetaMvc.Infrastructure;
using NHibernate;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnetaMvc.Data.NHibernate.Repositories
{
    public class DynamicMenuRepository : BaseRepository, IDynamicMenuRepository
    {
        private readonly ISession _session;

        public DynamicMenuRepository(IMapperSession session)
            : base(((NHSession)session).GetCurrentSession())
        {
            _session = ((NHSession)session).GetCurrentSession();
        }

        public IEnumerable<Core.Domain.Content.DynamicMenu> GetAllRootMenus()
        {
            var result = _session.CreateCriteria<DynamicMenu>()
                .List<DynamicMenu>();
            return result.Where(x => x.Parent == null);
        }

        public void DeleteMenu(long menuId)
        {
            var obj = GetMenuById(menuId);
            if (obj == null)
                return;
            DeleteInTransaction(obj);
        }

        public void SaveMenu(Core.Domain.Content.DynamicMenu menu)
        {
            SaveInTransaction(menu);
        }

        public DynamicMenu GetMenuById(long id)
        {
            return _session.Get<DynamicMenu>(id);
        }
    }
}
